﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Resources;
using Microsoft.Phone.Controls;
using Newtonsoft.Json.Linq;

namespace WindowsPhoneAppHost
{
    public partial class DaiBanList : PhoneApplicationPage
    {
        private int totalPage = 100;
        private int currentPage = 0;
        public DaiBanList()
        {
            InitializeComponent();
            DoHttpWebRequest();
            controlPanel.TotalPage = totalPage;
        }
        
        /*
        private void AddItems()
        {
            for (int i = 0; i < 50; i++)
            {
                ListBoxItem item = new ListBoxItem();
                TextBlock textBlock = new TextBlock();
                textBlock.Text = i.ToString();
                item.Content = textBlock;
                listBox1.Items.Add(item);
            }
        }
        */

        private void readFile()
        {
            string fileName = "/MobileOA;component/TodoList.json";
            StreamResourceInfo resourceInfo = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));

            using (var reader = new StreamReader(resourceInfo.Stream))
            {
                //textBlock1.Text = reader.ReadToEnd();
                //reader.Close();
            }
        }

        private void DoHttpWebRequest()
        {
            //string url = "http://www.cnblogs.com/linzheng";
            //string url = "http://localhost:8080/MobileOA/TodoList?username=lihua&password=pass";
            //string url = "http://localhost:8080/MobileOA/TodoList?username=admin&password=pass";
            string url = "http://localhost/MobileOA/DaiBan.json";
            WebRequest request = WebRequest.Create(url); //创建WebRequest类
            IAsyncResult result = request.BeginGetResponse(ResponseCallback, request); //返回异步操作的状态
        }

        private void ResponseCallback(IAsyncResult result)
        {
            var request = (HttpWebRequest) result.AsyncState; //获取异步操作返回的的信息
            WebResponse response = request.EndGetResponse(result); //结束对 Internet 资源的异步请求
            using (Stream stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                string contents = reader.ReadToEnd();
                //Dispatcher.BeginInvoke(delegate() { this.textBlock1.Text = contents; });
                Dispatcher.BeginInvoke(delegate { ParseJson(contents); });
            }
        }

        private void ParseJson(string jsonString)
        {
            Debug.WriteLine(jsonString);
            //JObject json = JObject.Parse(jsonString);
            //*
            JArray jArray = JArray.Parse(jsonString);
            Debug.WriteLine(jArray.Count.ToString());
            listBox1.Items.Add(new ListSeperator());
            for (int i = 0; i < jArray.Count; i++)
            {
                var obj = (JObject)jArray[i];
                if (null != obj["Subject"])
                {
                    //ListBoxItem item = new ListBoxItem();
                    //item.
                    //var item = new OaTypeItem();
                    //var textBlock = new TextBlock();
                    //textBlock.Text = obj["Subject"].ToString();
                    MyListBoxItem content = new MyListBoxItem();
                    content.Subject = obj["Subject"].ToString();
                    content.From = obj["From"].ToString();
                    content.Time = obj["Time"].ToString();
                    content.Did = obj["did"].ToString();
                    content.ButtonClick = "ItemSelected";
                    //item.Content = content;
                    listBox1.Items.Add(content);
                    listBox1.Items.Add(new ListSeperator());
                }
            }
        }

        public void Refresh(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Refresh");
        }
        public void Search(object sender, RoutedEventArgs e)
        {

        }
        public void GetPrePage(object sender, RoutedEventArgs e)
        {
            controlPanel.CurrentPage = --currentPage;
        }

        public void GetNextPage(object sender, RoutedEventArgs e)
        {
            controlPanel.CurrentPage = ++currentPage;
        }

        public void ItemSelected(object sender, RoutedEventArgs e)
        {
            MyListBoxItem item = (MyListBoxItem)sender;
            MessageBox.Show(item.Did);
        }
    }
}